package org.ocean.hibernate;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import org.hibernate.cfg.Configuration;
import org.hibernate.impl.SessionFactoryImpl;
import org.hibernate.mapping.Table;
import org.ocean.online.auth.AuthManager;
import org.ocean.online.sys.OnlineConfig;

public class SessionFactoryUtil {

	private static SessionFactoryImpl sysSessionFactory;
	private static Configuration config;

	public static void init(){
		
		config = new Configuration();
		config.setProperty("hibernate.show_sql", "true");
		config.setProperty("hibernate.format_sql", "true");
		initSqlite(config);
		// 初始化系统表映射
		config.addInputStream(SessionFactoryUtil.class.getResourceAsStream("sys.hbm.xml"));
		sysSessionFactory = (SessionFactoryImpl) config.buildSessionFactory();
		Iterator iter = config.getTableMappings();
		while(iter.hasNext()){
			try{
				CreateTableUtil.createTable((Table)iter.next(),config);
			}catch(Exception ex){
				System.out.println(ex.getMessage());
			}
		}
		AuthManager.getInstance();
	}
	
	private static void initAccess(Configuration config){
//		config.setProperty("hibernate.connection.url", "jdbc:access:e:\\dbspace\\test.db");
		config.setProperty("hibernate.connection.url", "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=e:\\dbspace\\test.mdb");
//		config.setProperty("hibernate.connection.driver_class", "com.hxtt.sql.access.AccessDriver");
		config.setProperty("hibernate.dialect", "com.hxtt.support.hibernate.HxttAccessDialect");
	}
	private static void initDerby(Configuration config){
		config.setProperty("hibernate.connection.url", "jdbc:derby:e:\\derbydata\\test.db");
		config.setProperty("hibernate.connection.driver_class", "org.apache.derby.jdbc.EmbeddedDriver");
		config.setProperty("hibernate.dialect", "org.hibernate.dialect.DerbyDialect");
	}
	
	private static void initFirebird(Configuration config){
//		config.setProperty("hibernate.connection.username", "root");
//		config.setProperty("hibernate.connection.password", "admin");
		config.setProperty("hibernate.connection.url", "jdbc:firebirdsql:embedded:e:\\fbdata\\test.fdb");
		config.setProperty("hibernate.connection.driver_class", "org.firebirdsql.jdbc.FBDriver");
		config.setProperty("hibernate.dialect", "org.hibernate.dialect.FirebirdDialect");
//		config.setProperty("hibernate.connection.provider_class", "org.firebirdsql.pool.FBSimpleDataSource");
	}
	
	private static void initMySql(Configuration config){
		config.setProperty("hibernate.connection.username", "root");
		config.setProperty("hibernate.connection.password", "admin");
		config.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/test");
		config.setProperty("connection.connection.driver_class", "com.mysql.jdbc.Driver");
		config.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
	}
	
	private static void initHsql(Configuration config){
		config.setProperty("hibernate.connection.url", "jdbc:hsqldb:file:"+OnlineConfig.getProperty("onlinedata_dir")+File.separator+"system.hs.db");
		config.setProperty("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver");
		config.setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
	}
	private static void initH2(Configuration config){
		config.setProperty("hibernate.connection.url", "jdbc:h2:file:"+OnlineConfig.getProperty("onlinedata_dir")+File.separator+"system.db");
		config.setProperty("hibernate.connection.driver_class", "org.h2.Driver");
		config.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
	}
	private static void initSqlite(Configuration config){
		config.setProperty("hibernate.connection.url", "jdbc:sqlite:"+OnlineConfig.getProperty("onlinedata_dir")+File.separator+"system.db");
		config.setProperty("hibernate.connection.driver_class", "org.sqlite.JDBC");
		config.setProperty("hibernate.dialect", "com.applerao.hibernatesqlite.dialect.SQLiteDialect");
	}
	public static SessionFactoryImpl getSysSessionFactory(){
		return sysSessionFactory;
	}
	
	public static Configuration getConfig(){
		return config;
	}
}
